package com.yu.mall.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @Desc: 公共返回对象
 * @Author: zhangyicheng
 * @Date: 2021/3/10
 */
@Getter
@Setter
public class ResultMessage<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String SUCCESS_CODE = "0";
    public static String SUCCESS_MSG = "成功";
    public static String FAIL_CODE = "1";
    public static String FAIL_MSG = "失败";
    public static String ERROR_CODE = "2";
    public static String ERROR_MSG = "异常";

    private Boolean result = Boolean.valueOf(true);
    private String code = SUCCESS_CODE;
    private String msg = SUCCESS_MSG;
    private T data;
    private Long count;

    public ResultMessage() {
    }

    public ResultMessage(Boolean result, String code, String msg, T data) {
        this.result = result;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultMessage(Boolean result, String code, String msg, T data, Long count) {
        this.result = result;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.count = count;
    }

    public static <TT> ResultMessage<TT> success(String msg, TT data) {
        return new ResultMessage<TT>(Boolean.TRUE, SUCCESS_CODE, msg, data);
    }

    public static <TT> ResultMessage<TT> success(TT data) {
        return new ResultMessage<TT>(Boolean.TRUE, SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <TT> ResultMessage<TT> success(String msg) {
        return new ResultMessage<TT>(Boolean.TRUE, SUCCESS_CODE, msg, null);
    }

    public static <TT> ResultMessage<TT> success(String msg, TT data, Long count) {
        return new ResultMessage<TT>(Boolean.TRUE, SUCCESS_CODE, msg, data, count);
    }

    public static <TT> ResultMessage<TT> success(TT data, Long count) {
        return new ResultMessage<TT>(Boolean.TRUE, SUCCESS_CODE, SUCCESS_MSG, data, count);
    }

    public static <TT> ResultMessage<TT> success(String msg, Long count) {
        return new ResultMessage<TT>(Boolean.TRUE, SUCCESS_CODE, msg, null, count);
    }

    public static <TT> ResultMessage<TT> fail(String msg, TT data) {
        return new ResultMessage<TT>(Boolean.FALSE, FAIL_CODE, msg, data);
    }

    public static <TT> ResultMessage<TT> fail(TT data) {
        return new ResultMessage<TT>(Boolean.FALSE, FAIL_CODE, FAIL_MSG, data);
    }

    public static <TT> ResultMessage<TT> fail(String msg) {
        return new ResultMessage<TT>(Boolean.FALSE, FAIL_CODE, msg, null);
    }

    public static <TT> ResultMessage<TT> error(String msg, TT data) {
        return new ResultMessage<TT>(Boolean.FALSE, ERROR_CODE, msg, data);
    }

    public static <TT> ResultMessage<TT> error(TT data) {
        return new ResultMessage<TT>(Boolean.FALSE, ERROR_CODE, ERROR_MSG, data);
    }

    public static <TT> ResultMessage<TT> error(String msg) {
        return new ResultMessage<TT>(Boolean.FALSE, ERROR_CODE, msg, null);
    }

    public static <TT> ResultMessage<TT> result(Boolean result, String code, String msg, TT data) {
        return new ResultMessage<TT>(result, code, msg, data);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue);
    }
}